package cz.matasek.dbLogger.gui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;

/**
 * This class is a basic implementation of the {@link javax.swing.ListModel}
 * for {@link java.lang.String} objects.
 *
 * @author Luboš Matásek
 */
public class StringListModel implements ListModel {

    /**
     * List of objects.
     */
    private List<String> objects;

    /**
     * Creates a new <code>StringListModel</code> from a collection of strings.
     *
     * @param objects the collection of string to be added
     */
    public StringListModel(Collection<String> objects) {
        this.objects = new ArrayList<String>(objects);
    }

    /**
     * Returns the length of the list.
     *
     * @return the length of the list
     */
    @Override
    public int getSize() {
        return objects.size();
    }

    /**
     * Returns the value at the specified index.
     *
     * @param index the requested index
     * @return the value at <code>index</code>
     */
    @Override
    public Object getElementAt(int index) {
        return objects.get(index);
    }

    /**
     * Empty implementation.
     * 
     * @param l the <code>ListDataListener</code> to be added
     */
    @Override
    public void addListDataListener(ListDataListener l) {
    }

    /**
     * Empty implementation.
     *
     * @param l the <code>ListDataListener</code> to be removed
     */
    @Override
    public void removeListDataListener(ListDataListener l) {
    }

    /**
     * Adds a new element into the list.
     *
     * @param newElement the element to be added
     */
    public void addElement(String newElement) {
        objects.add(newElement);
    }

    /**
     * Returns elements in the list.
     *
     * @return the collection of elements in the list
     */
    public Collection<String> getElements() {
        return new ArrayList<String>(objects);
    }
}
